package com.johnson.news.fragment;


import android.os.Bundle;

import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentStatePagerAdapter;
import androidx.viewpager.widget.ViewPager;
import io.reactivex.functions.Consumer;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.androidkun.xtablayout.XTabLayout;
import com.google.android.material.tabs.TabLayout;
import com.johnson.news.R;
import com.johnson.news.adapter.MyFragmentPagerAdapter;
import com.johnson.news.model.bean.DataNewsCategory;
import com.johnson.news.model.remote.RemoteDataSource;
import com.johnson.news.util.Utils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link HomeFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class HomeFragment extends Fragment {
    // TODO: Rename parameter arguments, choose names that match
    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";

    // TODO: Rename and change types of parameters
    private String mParam1;
    private String mParam2;


    public HomeFragment() {
        // Required empty public constructor
    }

    /**
     * Use this factory method to create a new instance of
     * this fragment using the provided parameters.
     *
     * @param param1 Parameter 1.
     * @param param2 Parameter 2.
     * @return A new instance of fragment HomeFragment.
     */
    // TODO: Rename and change types and number of parameters
    public static HomeFragment newInstance(String param1, String param2) {
        HomeFragment fragment = new HomeFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
    }

    View rootView;
    XTabLayout home_tab;
    ViewPager home_viewpager;
    MyFragmentPagerAdapter adapter;

    ArrayList<String> titleList = new ArrayList<>();
    ArrayList<Fragment> fragmentList = new ArrayList<>();
    ArrayList<Integer> catIdList = new ArrayList<>();
    ArrayList<String> mIdList = new ArrayList<>();

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        if (rootView == null) {
            rootView = inflater.inflate(R.layout.fragment_home, container, false);
            init();
        }

        return rootView;
    }

    void init() {
        home_tab = rootView.findViewById(R.id.home_tab);
        home_viewpager = rootView.findViewById(R.id.home_viewpager);
        adapter = new MyFragmentPagerAdapter(getActivity().getSupportFragmentManager(), FragmentStatePagerAdapter.BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT);
        home_viewpager.setAdapter(adapter);

        home_tab.setupWithViewPager(home_viewpager);

        home_viewpager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {

            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

//        titleList.add("最新");
//        titleList.add(getString(R.string.main_news_cat_1));
//        titleList.add(getString(R.string.main_news_cat_2));
//        titleList.add(getString(R.string.main_news_cat_3));
//        titleList.add(getString(R.string.main_news_cat_4));
//        titleList.add(getString(R.string.main_news_cat_5));
//
//        catIdList.add(0);
//        catIdList.add(426);
//        catIdList.add(427);
//        catIdList.add(428);
//        catIdList.add(429);
//        catIdList.add(435);
//
//        for (int i = 0; i < titleList.size(); i++) {
//            fragmentList.add(NewsListFragment.newInstance(catIdList.get(i), null));
//        }
//
//        adapter.setData(fragmentList, titleList);

        getData();
    }

    public void refreshCurrentPage() {
        if(fragmentList.size() == 0) {
            getData();
        } else {
            ((NewsListFragment) fragmentList.get(home_viewpager.getCurrentItem())).refreshData();
        }
    }

    void getData() {
        RemoteDataSource.getRemoteDataSource()
                .getNewsCats()
                .subscribe(new Consumer<ArrayList<DataNewsCategory>>() {
                    @Override
                    public void accept(ArrayList<DataNewsCategory> dataNewsCategories) throws Exception {
//                        titleList.add("最新");
//                        catIdList.add(0);
//                        mIdList.add("");

                        if (dataNewsCategories != null && dataNewsCategories.size() > 0) {
                            Collections.sort(dataNewsCategories, new Comparator<DataNewsCategory>() {
                                @Override
                                public int compare(DataNewsCategory o1, DataNewsCategory o2) {
                                    return o1.order - o2.order;
                                }

                                @Override
                                public boolean equals(Object obj) {
                                    return false;
                                }
                            });

                            titleList.clear();
                            fragmentList.clear();

                            for (DataNewsCategory category : dataNewsCategories) {
                                titleList.add(category.name);
//                                catIdList.add(category.catid);
//                                mIdList.add(category.mid);
                                fragmentList.add(NewsListFragment.newInstance(Utils.parseInt(category.catid), category.mid));
                            }
                        }
//                        for (int i = 0; i < titleList.size(); i++) {
//                            fragmentList.add(NewsListFragment.newInstance(catIdList.get(i), mIdList.get(i)));
//                        }

                        adapter.setData(fragmentList, titleList);
                    }
                });
    }

}
